<?php

$deId = $_GET['pid'] > 0 ? $_GET['pid'] : $_POST['deId'];

if ($deId < 1 || $deId == '') {

    header('Location: categorie_list.php');
    exit;
}

error_reporting(E_ALL);
include_once('../controller/includes/constants.php');
include_once('../controller/includes/db_access.php');

if ((isset($_POST['action']) ? $_POST['action'] : (isset($_GET['action']) ? $_GET['action'] : '')) == 'multiple') {

    $categorie = stripslashes($_POST['categorie']);
    $descr = stripslashes($_POST['descr']);
    $categorie_id = $_POST['deId'];

    $q = new Query;
    $q
            ->update('`categorieen`')
            ->set(
                    array(
                        '`categorieen`.`categorie`' => $categorie,
                        '`categorieen`.`descr`' => $descr
                    )
            )
            ->from('`categorieen`')
            ->where_equal_to(
                    array(
                        '`categorieen`.`categorie_id`' => $categorie_id
                    )
            )
            ->limit(1);

    $result = $q->run();
    $count = $q->get_selected_count();

    if (!($result && $count > 0)) {
        $Error = 'No category found.' . "\n";
    } else {

           header('Location: categorie_list.php');
           
    }
}
?>

<?php
if(isset($_GET['pid'])){

$id = $_GET['pid'];
$q = new Query;
$q
        ->select('*'
        )
        ->from('`categorieen`')
        ->where_equal_to(
                array(
                    '`categorieen`.`categorie_id`' => $id,
                )
);

$result = $q->run();

if (!($result > 0)) {
    $Error = 'No category found.' . "\n";
} else {
    $rows = mysql_fetch_object($result);
}}
?>


<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>               
        <table border="0" cellspacing="5" cellpadding="5">
            <thead>
                <tr>
                    <th colspan="2"><?php include_once('../controller/includes/header.php'); ?></th>
                </tr>
            </thead>
            <tbody>
            <form name="form1" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
                <input type="hidden" name="action" value="multiple" />  
                <input type="hidden" name="deId" value="<?php echo $deId; ?>" /> 
                <tr>
                    <td colspan="2"><h1>categorie edit</h1><br/><br/>
<?php
if (isset($Error)) {
    echo $Error;
}
?></td>
                </tr>
                <tr>
                    <td valign="top">Categorie naam:
                    </td>
                    <td valign="top">
                        <input type="text" name="categorie" value="<?php echo $rows->categorie; ?>" size="20" />

                    </td>
                </tr>
                <tr>
                    <td valign="top">beschrijving:<br />

                    </td>
                    <td valign="top">
                        <textarea name="descr" rows="4" cols="20">
<?php echo $rows->descr; ?>
                        </textarea>

                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit" value="save" name="save" />
                    </td>
                </tr>
            </form></tbody>
    </table>    

</body>
</html>
